package com.huanying.goodjob.community;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huanying.framework.BaseController;
import com.huanying.framework.PageBean;

@Controller
public class CommunityController extends BaseController{
	private static Logger logger = Logger.getLogger(CommunityController.class);

	@Autowired
	private HttpServletResponse response;
	@Autowired
	private CommunityService communityService;
	@RequestMapping(value = "/show_add_community.do")
	public String show_xiaoquInfo(String province, String city, String county, String name, String page_num,String status,Model model) {
		Map<String, String> map = new HashMap<String, String>();
		PageBean pageBean = null;
		map.put("province", province);
		map.put("city", city);
		map.put("xqname", name);
		map.put("county", county);
		map.put("status", status);
		if (page_num == null) {
			page_num = "1";
		}

		try {
			pageBean = communityService.searchCommunity(map, super.getPageSize(), Integer.valueOf(page_num));
		} catch (NumberFormatException e) {
			logger.error("查询小区出错", e);
			// TODO Auto-generated catch block
			e.getMessage();
		} catch (Exception e) {
			logger.error("查询小区出错", e);
			// TODO Auto-generated catch block
			e.getMessage();
		}
		model.addAttribute("name",name);
		model.addAttribute("communities", pageBean.getList());
		model.addAttribute("page", pageBean);
		model.addAttribute("pageSize", pageBean.getPageSize());
		return "/community/add_community";
	}

	//查找区县下的所有小区
		@RequestMapping(value="/findAll.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
		@ResponseBody 
		public String findAll_community(int county_code ,String jsoncallback) {
			response.addHeader( "Access-Control-Allow-Origin", "*" );

			logger.info(county_code);
			String json;
			try {
				List<Community> list = new ArrayList<Community>();
				list=communityService.getbyCountyCode(county_code);
				
				if(list!=null && list.size()!=0) {
					json = "{\"msg\":{\"flag\":\"1\"},[";
					
					for(int i=0;i<list.size();i++){
						json += "{\"name\":\""+list.get(i).getName()+"\"},";
					}
					json = json.substring(0,json.length()-1);
					json += "]}";
					logger.info("查找成功");
				}
				else {
					 //map.put("flag", "2");
					json = "{\"msg\":{\"flag\":\"2\"}}";
				     logger.info("查找失败");}
					//listNew.add(map);}
			} catch (Exception e) {
				
				json = "{\"msg\":{\"flag\":\"3\"}}";
			    logger.error("发送给前台操作失败");
			}
			/*Gson gson=new Gson();
			String str=gson.toJson(listNew);
			return jsoncallback+"("+str+")";*/
			return jsoncallback+"("+json+")";
		}
	
	
	@RequestMapping(value = "/add_community.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> save(@RequestBody Community community,String province_code, String city_code, String county_code,String status) {
		Map<String, String> map = new HashMap<String, String>();		
		try {
			Area province = communityService.getbyCode(province_code);
			community.setProvince(province);
			Area city = communityService.getbyCode(city_code);
			community.setCity(city);
			Area county = communityService.getbyCode(county_code);
			community.setCounty(county);
			if (community.getId() == 0) {
				community.setStatus(1);
				communityService.add(community);
			} else {
				community.setStatus(Integer.parseInt(status));
				communityService.update(community);
			}
			map.put("flag", "1");
			map.put("id", String.valueOf(community.getId()));
		} catch (Exception e) {
			logger.error("添加小区失败", e);
			map.put("flag", "0");
		}
		return map;
	}

	@RequestMapping(value = "/get_community/{id}/.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> get_community(@PathVariable("id") int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Community c = communityService.getbyId(id);
			map.put("flag", "1");
			map.put("community", c);
		} catch (Exception e) {
			logger.error("获得小区失败", e);
			map.put("flag", "0");
		}

		return map;
	}

	@RequestMapping(value = "/get_province.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> get_province() {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Area> list = new ArrayList<Area>();
			list = communityService.getProvince();
			map.put("flag", "1");
			map.put("area", list);
		} catch (Exception e) {
			logger.error("获得省失败", e);
			map.put("flag", "0");
		}

		return map;
	}

	@RequestMapping(value = "/get_city/{code}/.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> get_city(@PathVariable("code") String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Area> list = new ArrayList<Area>();
			list = communityService.getCity(code);
			map.put("flag", "1");
			map.put("area", list);
		} catch (Exception e) {
			logger.error("获得城市失败", e);
			map.put("flag", "0");
		}

		return map;
	}

	@RequestMapping(value = "/get_county/{code}/.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> get_county(@PathVariable("code") String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Area> list = new ArrayList<Area>();
			list = communityService.getCounty(code);
			map.put("flag", "1");
			map.put("area", list);
		} catch (Exception e) {
			logger.error("获得县失败", e);
			map.put("flag", "0");
		}

		return map;
	}

	@RequestMapping(value = "/delete_community.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> delete_community(int id) {
		int flog = 0;
		System.out.print(id);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			flog = communityService.delete_community(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("删除小区失败", e);
		}
		map.put("flog", flog);
		return map;
	}
}
